package Main.CS2.client;

import Main.CS2.entity.Contact;
import Main.CS2.dao.ContactDao;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ContactUpdateJFrame {

    JFrame jf;
    JPanel jp;
    Font font;
    private Integer currentCCode;

    JTextField cnameText;
    JTextField caddressText;
    JTextField cphoneText;

    public ContactUpdateJFrame() {
        jf = new JFrame("修改联系人信息");
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗口关闭
        font = new Font("宋体", Font.BOLD, 16);
        jf.setSize( 370, 350);// 设置窗口位置和大小
        jf.setBackground(Color.gray);
    }

    public void display(Integer currentCCode) {
        this.currentCCode=currentCCode;
        jp = getJp();
        jf.add(jp);
        //显示窗口（把内存中的窗口显示在屏幕上）
        jf.setVisible(true);
    }

    private JPanel getJp(){
        JPanel jp = new JPanel();
        jp.setLayout(null);

        JLabel tip1 = new JLabel("姓名:");
        tip1.setFont(font);
        tip1.setBounds(30,0,100,100);
        jp.add(tip1);

        cnameText = new JTextField(20);
        cnameText.setBounds(100,30,200,40);

        jp.add(cnameText);

        JLabel tip2 = new JLabel("地址:");
        tip2.setFont(font);
        tip2.setBounds(30,70,100,100);
        jp.add(tip2);

        caddressText = new JTextField(20);
        caddressText.setBounds(100,100,200,40);
        jp.add(caddressText);

        JLabel tip3 = new JLabel("电话:");
        tip3.setFont(font);
        tip3.setBounds(30,140,100,100);
        jp.add(tip3);

        cphoneText = new JTextField(20);
        cphoneText.setBounds(100,170,200,40);
        jp.add(cphoneText);

        JButton Button1 = new JButton("确定");
        Button1.setBounds(50,220,100,60);
        Button1.setFont(font);
        jp.add(Button1);

        JButton Button2 = new JButton("取消");
        Button2.setBounds(190,220,100,60);
        Button2.setFont(font);
        jp.add(Button2);

        Button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SqlSession session = MySqlSession.getSession();
                ContactDao contactMapper = session.getMapper(ContactDao.class);

                Contact contact = new Contact();
                contact.setCcode(currentCCode);
                contact.setCname(cnameText.getText());
                contact.setCaddress(caddressText.getText());
                contact.setCphone(cphoneText.getText());

                System.out.println("修改操作："+contact);

                contactMapper.contactUpdate(contact);
                //手动提交事务
                session.commit();
                jf.dispose();
                JFrameManager.mainJFrame.SearchButton.doClick();
                JOptionPane.showMessageDialog(null, "修改成功", "消息", JOptionPane.OK_OPTION);
            }
        });

        Button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jf.dispose();
            }
        });

        return jp;
}}
